package com.morontestandroid;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.Display;
import android.view.View;

import android.widget.ImageView;
import android.widget.TextView;

public class SecondLevel extends Activity implements android.view.View.OnClickListener{
	ImageView earth1;
	ImageView jupiter1;
	ImageView mercurius1;
	ImageView mars1;
	Chronometer mChronometer;
	ImageView backgr;
	TextView earthText, jupiterText, mercuryText, marsText, question;
	boolean firstObject, mercuryClicked, marsClicked, earthClicked, jupiterClicked;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_level);
        
        earth1 = (ImageView) findViewById(R.id.earthId);
        mChronometer = (Chronometer) findViewById(R.id.chmId);
        jupiter1 = (ImageView) findViewById(R.id.jupiterId);
        mercurius1 = (ImageView) findViewById(R.id.mercuriusId);
        mars1 = (ImageView) findViewById(R.id.marsId);
        backgr = (ImageView) findViewById(R.id.level2BackgId);
        
        earthText = (TextView) findViewById(R.id.earthTextId);
        jupiterText = (TextView) findViewById(R.id.jupiterTextId);
        mercuryText = (TextView) findViewById(R.id.mercuryTextId);
        marsText = (TextView) findViewById(R.id.marsTextId);
        question = (TextView) findViewById(R.id.level2QuestionId);
        mChronometer.setBase(getTime());          
		mChronometer.start();
		
		firstObject = true;
		mercuryClicked = false;
		marsClicked = false;
		earthClicked = false;
		jupiterClicked = false;
        
		earth1.setOnClickListener(this);
        jupiter1.setOnClickListener(this);
        mercurius1.setOnClickListener(this);
        mars1.setOnClickListener(this);
        backgr.setOnClickListener(this);
     
        
        mercurius1.bringToFront();
        
	       Typeface font = Typeface.createFromAsset(getAssets(), "testtext.ttf");  
	        question.setTypeface(font);
	        earthText.setTypeface(font);
	        jupiterText.setTypeface(font);
	        mercuryText.setTypeface(font);
	        marsText.setTypeface(font);
	}

	public long getTime() {
		  Bundle extras = getIntent().getExtras();
		  long time = extras.getLong("time");
		  return time;
		 
	}
	
	@Override
	public void onClick(View view) {
		Dialog d = new Dialog(this);
		
		if(view.getId()==R.id.level2BackgId){
			startActivity(new Intent(this, FailActivity.class));
		}
		else if(view.getId()==R.id.jupiterId && firstObject) {
			jupiter1.setImageResource(R.drawable.jupiter2);
			jupiterText.setVisibility(View.INVISIBLE);
			earth1.bringToFront();
			jupiterClicked = true;
			firstObject=false;
			
		}
		else if(view.getId()==R.id.earthId && jupiterClicked){
			earthClicked = true;
			jupiterClicked = false;
			earth1.setImageResource(R.drawable.earth2);
			earthText.setVisibility(View.INVISIBLE);
			question.setVisibility(View.INVISIBLE);
			mars1.bringToFront();
			
		}
		
		else if(view.getId()==R.id.marsId && earthClicked){
			earthClicked = false;
			marsClicked = true;
			mars1.setImageResource(R.drawable.mars2);
			marsText.setVisibility(View.INVISIBLE);
			mercurius1.bringToFront();
		}
		else if(view.getId()==R.id.mercuriusId && marsClicked){
			marsClicked = false;
			mercurius1.setImageResource(R.drawable.mercurius2);
			mercuryText.setVisibility(View.INVISIBLE);
			int activityID = 1;
			Intent intent;
			intent = new Intent().setClass(this, ThirdLevel.class);
			intent.putExtra("time", getThisBase());
			startActivityForResult(intent, activityID);
		}
		
		else {
			startActivity(new Intent(this, FailActivity.class));
			//d = new Dialog(this);
			//d.setTitle("fail");
			//d.show();
		}
		
	}
	 public void onResume(){
		    super.onResume();
		    
	 }
	public void onBackPressed() {
		// do something on back
		startActivity(new Intent(this, MenyActivity.class));
		return;
		}
	
	public long getThisBase(){
		  return mChronometer.getBase();
		 
	}
	
}
